<?php 
header('content-type:text/html;charset=utf8');
// print_r($_FILES);
$file = $_FILES['file'];
$ext = strrchr($file['name'],'.');
$fileName = time().rand(10000,99999).$ext;
//把文件保存到指定的目录
$bool = move_uploaded_file($file['tmp_name'],'../../static/uploads/'.$fileName);
//如果图片上传成功，返回的路径，让前端页面可以预览
$response = ["code"=>0,"msg"=>"操作失败"];
if($bool){
    $response["code"] = 1;
    $response["msg"] = "操作成功";
    //把上传的图片的路径带回前端
    $response["src"] = "/static/uploads/".$fileName;
}
header('content-type:application/json;charset=utf8');
echo json_encode($response);
?>